import request from '@/utils/request'
import format from 'date-format'

export function fetchGetSildeData(params) {
  return request({
    url: 'product/findProductall',
    method: 'post',
    params
  }).then(result=>{
    const res = {}
    res.data = []
    result.data.forEach(item=>{
      console.log(item)
      if(item.lzWebProductLevel2Dos.length >0){
        res.data.push(item)
      }
    })
    return res
  })
}


export function fetchGetProduct(params) {
  return request({
    url: 'product/findProductById',
    method: 'post',
    params
  }).then(result=>{
    if(result.status === 200){
      result.data.lzWebProductDo.summary = result.data.lzWebProductDo.summary.split(',')
      return result
    }else{
      return result
    }
   
  })
}

export function fetchGetSearch(params) {
  return request({
    url: 'product/findProductByName',
    method: 'post',
    params
  })
}

export function fetchAddOrder(data) {
  return request({
    url: 'order/addOrder',
    method: 'post',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    data: {
      type: 2,
      data: data
    }
  })
}
export function fetchOrderList(params) {
  return request({
    url: 'order/findOrderByUserId',
    method: 'post',
    params
  }).then(result=>{
    const res = {}
    res.orderList = []
    result.data.content.forEach(item=>{
      const data = {}
      data.consumerAddress = item.consumerAddress
      data.createTime = format('yyyy.MM.dd hh:mm:ss',new Date(item.createTime)) 
      data.orderSeq = item.orderSeq
      data.totalPrices = 0
      data.numbers = 0
      data.orderName = item.orderName
      data.img = item.img
      item.lzWebOrderDetailDos.forEach(item=>{
        data.totalPrices =data.totalPrices + item.totalPrices
        data.numbers = data.numbers + item.numbers
      })
      res.orderList.push(data)
    })
    return res
  })
}

export function fetchNewsList(params) {
  return request({
    url: 'news/findAllNew',
    method: 'post',
    params
  }).then(result=>{
    const res = {}
    res.newsList = []
    result.data.forEach((item) => {
      const data = {}
      data.title = item.title
      data.content = item.content
      data.source = item.source
      data.newsImg = item.newsImg
      data.createTime = format('yyyy.MM.dd', new Date(item.createTime))
      data.newsUrl = item.newsUrl
      data.describe = item.lzWebNewsCategoryDo.describe
      res.newsList.push(data)
    })
    return res
  })
}

export function fetchSearch(params) {
  return request({
    url: 'product/findProductByName',
    method: 'post',
    params
  }).then(result=>{
    return result.data
  })
}
